package com.bhprod.comatule;

import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bhprod.comatule.db.DBDataSource;
import com.bhprod.comatule.detail.SpeciesDetailActivity;
import com.bhprod.comatule.index.IndexActivity;
import com.bhprod.comatule.model.Photo;
import com.bhprod.comatule.model.Species;
import com.bhprod.comatule.quizz.QuizzActivity;
import com.bhprod.comatule.search.SearchByTaxonActivity;
import com.bhprod.comatule.util.Constants;
import com.bhprod.comatule.util.Tools;

public class MenuActivity extends Activity {

	private LinearLayout menuLayout;
	private Context context;
	private Photo currentPhoto;
	private int CHANGE_PHOTO_FREQUENCY = 3000;
	
	private Handler myHandler;
	
	private Runnable myRunnable = new Runnable() {
		@Override
		public void run() {
			generatePhoto();
			
		    myHandler.postDelayed(this,CHANGE_PHOTO_FREQUENCY);
	    }
	};

	/**
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.menu_page);

		context = this;

		int nbSpecies = DBDataSource.getInstance().countSpecies();
		TextView species = (TextView) findViewById(R.id.textMenuSpecies);
		species.setText(nbSpecies + " " + getString(R.string.nb_species) + " ");

		int nbPhotos = DBDataSource.getInstance().countPhotos();
		TextView photos = (TextView) findViewById(R.id.textMenuPhotos);
		photos.setText(" " + nbPhotos + " " + getString(R.string.nb_photos));

		menuLayout = (LinearLayout) findViewById(R.id.menuPage);

		myHandler = new Handler();
	    myHandler.postDelayed(myRunnable,0);
	}
	
	/**
	 * @see android.app.Activity#onPause()
	 */
	@Override
	public void onPause() {
	    super.onPause();
	    
	    if(myHandler != null)
	        myHandler.removeCallbacks(myRunnable);
	}

	public void displaySearchActivity(View view) {
		Intent intent = new Intent(this, SearchByTaxonActivity.class);
		startActivity(intent);
		finish();
	}

	public void displayIndexActivity(View view) {
		Intent intent = new Intent(this, IndexActivity.class);
		startActivity(intent);
		finish();
	}

	public void displayQuizzActivity(View view) {
		Intent intent = new Intent(this, QuizzActivity.class);
		startActivity(intent);
		finish();
	}

	private void generatePhoto() {
		currentPhoto = DBDataSource.getInstance().getRandomPhoto();

		String idpath = Tools.completeIdWithZeros(currentPhoto.getId(), getResources().getConfiguration().locale);

		String filename = Constants.PHOTOS_PATH + idpath + Constants.PHOTOS_EXTENSION;

		try {
			InputStream path = context.getAssets().open(filename);
			Bitmap bit = BitmapFactory.decodeStream(path);
			BitmapDrawable drawable = new BitmapDrawable(getResources(), bit);

			menuLayout.setBackgroundDrawable(drawable);
		} catch (IOException e) {
			Log.e(MenuActivity.class.getName(), "Cant't find file " + filename, e);
		}
	}
	
	public void displayDetail(View view) {
		Species currentSpecies = DBDataSource.getInstance().getSpeciesById(currentPhoto.getSpeciesId());
		Intent intent = new Intent(this, SpeciesDetailActivity.class);
		Bundle b = new Bundle();
		b.putInt("speciesId", currentSpecies.getId());
		intent.putExtras(b);
		startActivity(intent);
	}

}
